package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: libin
 * @date: 2019/11/25 10:27
 * @version: 1.0
 * @describe:
 */
@Service(interfaceClass = OrderSettingService.class )
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 添加预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //1.查找当前日期是否已经设置过预约人数
        long num = orderSettingDao.findNumberByDate(orderSetting.getOrderDate());
        if (num > 0) {
            //有就更新
            orderSettingDao.editNumberByDate(orderSetting);
        } else {
            //没有就添加
            orderSettingDao.addNumberByDate(orderSetting);
        }

    }


    /**
     * 从xlsx文件中批量导入预约
     * @param orderSettings
     */
    @Override
    public void upload(List<OrderSetting> orderSettings) {
       /* for (OrderSetting orderSetting : orderSettings) {
            //调用上面单个添加的方法
            editNumberByDate(orderSetting);
        }*/
        //优化，使用多线程导入
        //1.把数据拆分
        List<List<OrderSetting>> partition = Lists.partition(orderSettings, 20);
        //2.搞一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
        for (List<OrderSetting> settings : partition) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    for (OrderSetting setting : settings) {
                        editNumberByDate(setting);

                    }
                }
            });
        }

    }

    /**
     * 日历数据回显
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        Map map = new HashMap();
        String start = date+"-1";
        String end = date+"-31";
        map.put("start",start);
        map.put("end",end);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> maps=new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            //返回{"date":22,"mouth":8,"number":300,"reservations":300,}
            Map map2 = new HashMap();
            map2.put("date",orderSetting.getOrderDate().getDate());
            map2.put("mouth",orderSetting.getOrderDate().getMonth());
            map2.put("number",orderSetting.getNumber());
            map2.put("reservations",orderSetting.getReservations());
            maps.add(map2);
        }

        return maps;
    }

    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}
